package com.digitalchina.lxnsy.viewholder;

import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.entity.FarmingRecordVO;
import com.digitalchina.lxnsy.interfaces.IMyNhbPos;
import com.digitalchina.lxnsy.toolkit.JointImageUrl;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;

import java.util.ArrayList;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.viewholder
 * 创建人：郑皓
 * 创建时间：2018/8/28 20:10
 * 修改备注：
 * ================================================
 */

public class FarmingRecordFertilizerHolder extends BaseViewHolder<FarmingRecordVO> {

    private LinearLayout layoutMore;
    private TextView tvTime;
    private TextView tvType;
    private TextView tvWeather;
    private TextView tvWhq;

    private LinearLayout layoutZK;
    private LinearLayout layoutAddWater;
    private RelativeLayout layoutSQ;

    private LinearLayout layoutZKF;
    private LinearLayout layoutNoWater;
    private RelativeLayout layoutSQF;

    private NineGridView nineGridView;
    private TextView tvMsgInfo;


    private TextView tvDuiShui; // 兑水
    private TextView tvMu; // 亩

    private TextView tvEdit;
    private TextView tvDelete;


    private TextView tvOperation;

    private IMyNhbPos callBack;


    public FarmingRecordFertilizerHolder(ViewGroup parent, IMyNhbPos callBack) {
        super(parent, R.layout.item_farming_record);
        layoutMore = $(R.id.layout_more);
        tvTime = $(R.id.tv_time);
        tvType = $(R.id.tv_type);
        tvWeather = $(R.id.tv_weather);
        tvWhq = $(R.id.tv_whq);


        layoutZK = $(R.id.layout_zhankai);
        layoutSQ = $(R.id.layout_shouqi);

        layoutZKF = $(R.id.layout_zhankai_fl);
        layoutSQF = $(R.id.layout_shouqi_fl);

        layoutAddWater = $(R.id.layout_add_water);
        layoutNoWater = $(R.id.layout_no_water);

        nineGridView = $(R.id.nineGrid);
        tvMsgInfo = $(R.id.tv_msg_info);

        tvDuiShui = $(R.id.tv_duishui);
        tvMu = $(R.id.tv_mu);

        tvEdit = $(R.id.tv_edit);
        tvDelete = $(R.id.tv_delete);

        tvOperation = $(R.id.tv_operation);

        this.callBack = callBack;
    }

    @Override
    public void setData(final FarmingRecordVO data) {
        super.setData(data);

        // 公共部分
        tvTime.setText(data.getUpdateTime() + "");
        tvWeather.setText("天气：" + data.getWeather() + " " + data.getWeatherDescribe());
        // 物候期
        tvWhq.setText(data.getPhaseNameShow() + "");
        // 补充说明
        if (StringUtil.isStrEmpty(data.getSpecification())) {
            tvMsgInfo.setVisibility(View.GONE);
        } else {
            tvMsgInfo.setVisibility(View.VISIBLE);
            tvMsgInfo.setText(data.getSpecification() + "");
        }

        tvDuiShui.setText("兑水：" + data.getWaterTotal() + " 斤");
        tvMu.setText("亩数：" + data.getPerMuTotal() + " 亩");

        // 图片展示
        if (data.getFarmingOperationsimgs() != null && data.getFarmingOperationsimgs().size() > 0) {
            nineGridView.setVisibility(View.VISIBLE);
            ArrayList<ImageInfo> imageInfo = new ArrayList<>();

            for (int i = 0; i < data.getFarmingOperationsimgs().size(); i++) {
                ImageInfo info = new ImageInfo();
                info.setThumbnailUrl(JointImageUrl.smallImageUrl(data.getFarmingOperationsimgs().get(i).getImage()));
                info.setBigImageUrl(JointImageUrl.bigImageUrl(data.getFarmingOperationsimgs().get(i).getImage()));
                imageInfo.add(info);
            }
            nineGridView.setAdapter(new NineGridViewClickAdapter(getContext(), imageInfo));

        } else {
            nineGridView.setVisibility(View.GONE);
        }


        if ("1".equals(data.getCropType())) {
            // 类型为农药化肥
            layoutMore.setVisibility(View.VISIBLE);
            tvOperation.setVisibility(View.GONE);
            tvType.setText("用肥用药");


            if (data.getIspesticideFertilizers() != null && data.getIspesticideFertilizers().size() > 0) {
                layoutSQ.setVisibility(View.VISIBLE);
                layoutZK.setVisibility(View.GONE);
            } else {
                layoutSQ.setVisibility(View.GONE);
                layoutZK.setVisibility(View.GONE);
            }

            if (data.getNotpesticideFertilizers() != null && data.getNotpesticideFertilizers().size() > 0) {
                layoutSQF.setVisibility(View.VISIBLE);
                layoutZKF.setVisibility(View.GONE);
            } else {
                layoutSQF.setVisibility(View.GONE);
                layoutZKF.setVisibility(View.GONE);
            }

            //已经展开
            layoutZK.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    layoutZK.setVisibility(View.GONE);
                    layoutSQ.setVisibility(View.VISIBLE);
                }
            });
            //收起
            layoutSQ.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    layoutZK.setVisibility(View.VISIBLE);
                    layoutSQ.setVisibility(View.GONE);

                    layoutAddWater.removeAllViews();
                    layoutAddWater.removeAllViewsInLayout();
                    for (int i = 0; i < data.getIspesticideFertilizers().size(); i++) {
                        View view = View.inflate(getContext(), R.layout.item_farming_add_water, null);
                        TextView tvName = view.findViewById(R.id.tv_name);
                        TextView tvUse = view.findViewById(R.id.tv_use);
                        TextView tvMultiple = view.findViewById(R.id.tv_multiple);

                        tvName.setText(data.getIspesticideFertilizers().get(i).getName());
                        tvUse.setText("用量：" + data.getIspesticideFertilizers().get(i).getConsumption() + " " + data.getIspesticideFertilizers().get(i).getUnit());
                        tvMultiple.setText("稀释倍数：" + data.getIspesticideFertilizers().get(i).getMultiple() + " 倍");
                        layoutAddWater.addView(view);
                    }
                }
            });
            //已经展开-无需兑水
            layoutZKF.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    layoutZKF.setVisibility(View.GONE);
                    layoutSQF.setVisibility(View.VISIBLE);
                }
            });
            //收起-无需兑水
            layoutSQF.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    layoutZKF.setVisibility(View.VISIBLE);
                    layoutSQF.setVisibility(View.GONE);

                    layoutNoWater.removeAllViews();
                    layoutNoWater.removeAllViewsInLayout();
                    for (int i = 0; i < data.getNotpesticideFertilizers().size(); i++) {
                        View view = View.inflate(getContext(), R.layout.item_farming_no_water, null);
                        TextView tvName = view.findViewById(R.id.tv_name);
                        TextView tvUse = view.findViewById(R.id.tv_use);

                        tvName.setText(data.getNotpesticideFertilizers().get(i).getName());
                        tvUse.setText("用量：" + data.getIspesticideFertilizers().get(i).getConsumption() + " kg");
                        layoutNoWater.addView(view);
                    }
                }
            });

        } else {
            // 类型为农事操作
            layoutMore.setVisibility(View.GONE);
            tvType.setText("作物管理");

            if (data.getFarmingOperations() != null) {
                tvOperation.setVisibility(View.VISIBLE);
//                tvOperation.setText(data.getFarmingOperations().getFarmName() + "");
            } else {
                tvOperation.setVisibility(View.GONE);
            }

        }

        tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callBack.edit(getAdapterPosition());
            }
        });

        tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callBack.del(getAdapterPosition());
            }
        });
    }

}

